<html>
<head>
<title><?php $title = "Calculator";
echo "$title"; ?></title>
</head>

<body>
<h1 style="font-size:30px;"> 
<?php echo "Calculator";
?>
</h1>
<form method="get" action="calculator.php"> 
<input type="text" name="calcval" method="post">
<input type="submit" value="Calculate"/>

<h2 style="font-size:25px;">Result</h2>
<?php
error_reporting(0); 
$expression = $_GET["calcval"];

preg_match('/(-)?[\d]+(\.[\d]+)?((([\*\+\/](-)?)||(-))[\d]+(\.[\d]+)?)*/' , $expression, $matches);

if ($matches[0] == $expression)
{
eval('$calc= '.$expression.';');
echo $expression. " = " . $calc;
}
else{
echo "Invalid input expression " . $expression . ".";
}
?>
</body>
</html>
